"""
编程实现：循环输入10个人的信息（姓名，年龄，性别，身高），并存储到数据库中
create   table  person(
	username  varchar(20),
	age   int,
	sex 	  char(4),
	high  double(3,2)     # 总共3位数字，2位小数
)
"""


# import  pymysql
# db = pymysql.connect(host="127.0.0.1",user="root",password="root",database="Python_Database",port=3306,charset="utf8")
# cursor = db.cursor()
# sql = "insert into user values('张三',12,'男')"
# cursor.execute(sql)
# db.commit()
# cursor.close()
# db.close()

import pymysql

info = (("张三",12,"男",1.32),
        ("李四",13,"女",1.98),
        ("王五",14,"男",1.76),
        ("李梅",23,"女",1.56),
        ("东方欧柏",24,"男",1.88),
        ("李不会",32,"女",1.74),
        ("张梅",33,"女",1.77),
        ("苏白",18,"男",1.71),
        ("沈欣妍",24,"女",1.64),
        ("吴磊",28,"男",1.54))
#创建链接
db = pymysql.connect(host="127.0.0.1",user="root",password="root",database="Python_Database",port=3306,charset="utf8")
#创建游标
cursor = db.cursor()
#或者这样写
# with db.cursor() as cursor:#with自动关闭其中的资源，使用with可以不用写cursor.close，但是要注意使用缩进
#循环输入
for contain in info:
    try:
        sql = f"insert into user values{contain}"
        #执行sql
        cursor.execute(sql)
        #提交生效 select语句不需要提交
        db.commit()
    except Exception as e:
        print(sql)
#关闭资源，先创建的后关闭，后创建的先关闭
cursor.close()
db.close()